<?php
class Slide_Plugin extends Ot_Db_Table
{    
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_slide_plugin';
  
    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'pluginName';
    
    public function getEnabledPlugins()
    {
        $where = $this->getAdapter()->quoteInto('status = ?', 'enabled');
        
        $results = $this->fetchAll($where);
        
        if ($results->count() == 0) {
            return array();
        }
        
        $returnData = array();
        foreach ($results as $r) {
            $returnData[] = array(
                'name'      => $r->pluginName,
                'object'    => $this->factory($r->pluginName),
            );
        }
        
        return $returnData;        
    }
    
    public static function factory($pluginName)
    {       
        $classname = 'Slide_Plugin_' . $pluginName;
        
        set_include_path(get_include_path() . PATH_SEPARATOR . self::getPluginPath($pluginName));
        
        /*
         * Load the adapter class.  This throws an exception
         * if the specified class cannot be loaded.
         */
        require_once($pluginName . '.php');
        
        $adapter = new $classname();
        
        /*
         * Verify that the object created is a descendent of the abstract adapter type.
         */
        if (!$adapter instanceof Bbce_Slide_Plugin_Abstract) {
            throw new Exception("Plugin class '$classname' does not extend Bbce_Slide_Plugin_Abstract");
        }

        return $adapter;
    }
    
    /**
     * gets the basepath to the plugin
     * 
     * @param $plugin
     * @return string
     */
    public static function getPluginPath($pluginName = '')
    {
        return realpath(APPLICATION_PATH . '/../loadable/slideplugin/' . $pluginName);
    }
}